package com.dd.cloud.user.service.news.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dd.cloud.user.entity.news.NewsSee;
import com.dd.cloud.common.interceptor.UserInterceptor;
import com.dd.cloud.user.mapper.news.NewsSeeMapper;
import com.dd.cloud.user.service.news.INewsSeeService;
import com.dd.cloud.common.vo.LoginInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author Lx
 * @version 1.0
 */
@Slf4j
@Service
public class NewsSeeServiceImpl extends ServiceImpl<NewsSeeMapper, NewsSee> implements INewsSeeService {
    /**
     * 查询消息未读数量
     *
     * @return
     */
    @Override
    public Integer queryNewsSeeUnreadCount() {
        LoginInfo loginInfo = UserInterceptor.getLoginUserInfo().get();
        QueryWrapper<NewsSee> qw = new QueryWrapper<>();
        qw.lambda().eq(NewsSee::getUserId, loginInfo.getId())
                .eq(NewsSee::getUserType, loginInfo.getType())
                .eq(NewsSee::getStatus, 1);
        return count(qw);
    }

    /**
     * 查阅消息
     *
     * @param newId
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void seeNewInfo(Integer newId) {
        LoginInfo loginInfo = UserInterceptor.getLoginUserInfo().get();
        UpdateWrapper<NewsSee> uw = new UpdateWrapper<>();
        uw.lambda().eq(NewsSee::getNewId, newId)
                .eq(NewsSee::getUserId, loginInfo.getId())
                .eq(NewsSee::getUserType, loginInfo.getType())
                .set(NewsSee::getStatus, 2);
        update(uw);
    }
}
